/**************************************************************************//**
 * @item     CosyOS-III Kernel
 * @file     os_debug.c
 * @brief    DEBUG接口
 * @details  DEBUG接口，串口发送和接收解析。
 * @author   迟凯峰
 * @version  V1.2.5
 * @date     2025.06.17
 ******************************************************************************/

#include "os_var.h"
#if SYSCFG_DEBUGGING == __ENABLED__
#include "os_api.h"
#include "sv_create.h"
#include "sv_task.h"
#include "ur_api.h"
#include <string.h>

#if MCUCFG_ISA != __ARM__
/**
 \brief      字符串转整型
 \param[in]  str       字符串指针
 \return     转换后的整型数据
 \note       Keil C51/C251 标准库中的 atoi() 是非线程安全的，故额外定义此函数，
             专用于DEBUG接口的串口接收解析。
 */
static int _atoi_(const char* str)
{
	if(str == OS_NULL || !*str){
		return 0;
	}
	else{
		int a = 0;
		int k = 1;
		s_u8_t i = s_strlen(str);
		while(i--){
			if(str[i] >= '0' && str[i] <= '9'){
				a += (str[i] - '0') * k;
				k *= 10;
			}
			else if(str[i] == '+'){
				return a;
			}
			else if(str[i] == '-'){
				return -a;
			}
			else{
				return 0;
			}
		}
		return a;
	}
}
#else
#define _atoi_ atoi
#endif

/**
 \brief      接收解析
 \details    DEBUG接口，串口接收解析。
 \return     无
 */
void os_debug_recv(void)
{
	#define p   s_debug_recvptr
	#define len s_debug_recvlen
	p[len] = '\0';
	if(len == 9 && !s_strcmp(p, "taskmgr\r\n")){
		s_sign_taskmgr = true;
		s_taskmgr_upspeed = 0;
		s_timqry_handle[OS_TMID_TASKMGR]->timer = s_timqry_handle[OS_TMID_TASKMGR]->reload;
	}
	else if(len == 6 && !s_strcmp(p, "exit\r\n")){
		s_sign_taskmgr = false;
	}
	else if(len >= 15 && !s_memcmp(p, "taskmgr /s=", 11)){
		s_u8_t i;
		char *mptr = p + 11;
		for(i = 0; i < 5; i++){
			if(mptr[i] >= '0' && mptr[i] <= '9');
			else break;
		}
		if(i >= 2 && !s_strcmp(mptr + i, "\r\n")){
			s_u16_t temp;
			mptr[i] = '\0';
			temp = _atoi_(mptr);
			if(temp >= 50 && temp <= 5000){
				s_timqry_handle[OS_TMID_TASKMGR]->reload = (temp * 1000UL) / SYSCFG_SYSTICK_CYCLE;
			}
		}
	}
	#undef p
	#undef len
}

#define _taskname "\xCF\xB5\xCD\xB3\xB5\xF7\xCA\xD4\xC6\xF7" /*!< "系统调试器" */
uCreateTask_TimQry
(
	OS_TMID_DEBUGGER, s_debug_sendtype, false,
	Debugger, SYSCFG_TASKPRIORITY - 1, SYSCFG_STACKSIZE_DEBUGGER, 0, _taskname
)
{
	#if SYSCFG_DEBUG_SENDLEN == __DISABLED__
	void debug_hook(char _XDATA_MEM_ *p);
	#else
	void debug_hook(char _XDATA_MEM_ *p, s_u16_t len);
	#endif
	while(true){
		if(s_debug_sendtype & OS_DEBUG_SEND_TASKMGR){
			s_debug_sendtype &= ~OS_DEBUG_SEND_TASKMGR;
			#if SYSCFG_DEBUG_SENDLEN == __DISABLED__
			debug_hook(s_taskmgr_sendbuff);
			#else
			debug_hook(s_taskmgr_sendbuff, s_taskmgr_sendlen);
			#endif
		}/*
		else if(s_debug_sendtype & OS_DEBUG_SEND_CMDLINE){
			s_debug_sendtype &= ~OS_DEBUG_SEND_CMDLINE;
			#if SYSCFG_DEBUG_SENDLEN == __DISABLED__
			debug_hook(s_cmdline_sendbuff);
			#else
			debug_hook(s_cmdline_sendbuff, s_cmdline_sendlen);
			#endif
		}*/
		uSuspendTasking();
	}
}
#undef _taskname

#endif
